package com.yichenyang.ordersystem.controller;

import com.yichenyang.ordersystem.entity.Customer;
import com.yichenyang.ordersystem.entity.Product;
import com.yichenyang.ordersystem.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    CustomerService customerService;

    private int pageSize = 10;
    private int pageCount = 0;
    private int pageTotal;

    @RequestMapping("/list")
    public String memberList(HttpSession session) {
        session.setAttribute("page",1);
        int customersAll = customerService.queryAll();
        session.setAttribute("memberNum",customersAll);
        session.setAttribute("pages",getPages());

        List<Customer> customers = customerService.queryAllByLimit(0, pageSize);
        session.setAttribute("customers",customers);
        return "pages/member/list::memberList";
    }

    @PostMapping("/add")
    @ResponseBody
    public Customer memberAdd(@RequestBody Customer customer,HttpSession session) {
        customer.setCustomerStatus("1");
        customer.setJoinTime(getTime());
        customer = customerService.insert(customer);
        return customer;
    }

    @PostMapping("/find")
    public String memberFind(@RequestBody Map<String,String> map,HttpSession session) {
        if (session.getAttribute("customers")!=null) {
            List<Customer> customers = customerService.queryByConditions(map.get("username"), map.get("start"), map.get("end"));
            session.setAttribute("customers",customers);
        }
        return "pages/member/list::memberList";
    }

    //换页
    @PostMapping("/page/{page}")
    public String changePage(@PathVariable int page,HttpSession session) {
        pageCount = (page-1)*10;
        List<Customer> customers = customerService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("customers",customers);
        return "pages/member/list::memberList";
    }

    //更新页数
    @GetMapping("/updatePage")
    public String pageAdd(HttpSession session) {
        System.out.println(getPages());
        session.setAttribute("pages",getPages());
        return "pages/member/list::page";
    }

    //删除信息
    @GetMapping("/del/{id}")
    public String del(@PathVariable int id, HttpSession session) {
        customerService.deleteById(id);
        List<Customer> customers = customerService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("customers",customers);
        return "pages/member/list::memberList";
    }

    //启用用户
    @GetMapping("/invocation/{id}")
    @ResponseBody
    public void invocation(@PathVariable int id) {
        customerService.updateStatus(1,id);
    }

    //停用用户
    @GetMapping("/stop/{id}")
    @ResponseBody
    public void stop(@PathVariable int id) {
        customerService.updateStatus(0,id);
    }

    //修改用户信息
    @PostMapping("/update/{id}")
    public String update(@PathVariable int id,@RequestBody Customer customer) {
        customer.setCustomerId(id);
        customerService.update(customer);
        return "pages/member/list::memberList";
    }

    //获取用户信息
    @GetMapping("/message/{id}")
    @ResponseBody
    public Customer getMessage(@PathVariable int id) {
        Customer customer = customerService.queryById(id);
        return customer;
    }

    //更新最新时间
    public String getTime() {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = dateFormat.format(date);
        return now;
    }

    public List<Integer> getPages() {
        int customersAll = customerService.queryAll();
        pageTotal = customersAll / pageSize + (customersAll % pageSize == 0 ? 0:1);
        List<Integer> pages = new CopyOnWriteArrayList<>();
        for (int i = 0; i < pageTotal; i++) {
            pages.add(i+1);
        }
        return pages;
    }
}
